<template>
	<view class="container">
		<u-navbar title="设备报修" :autoBack="true" :fixed="true" :bgColor="'#ffffff'" :placeholder="true" :titleStyle="{
			color: '#333',
			fontSize: '16px',
		}"></u-navbar>

		<!-- 房源信息卡片 -->
		<view class="house-card">
			<view class="card-header">
				<view class="house-info">
					<image class="house-img" :src='detailInfo.house_images[0]'>
					</image>
					<view class="info-content">
						<view class="title">{{detailInfo.house_address}}</view>
						<view class="room-info">
							<text class="label">房间号：</text>
							<text class="value">{{detailInfo.room_name}}</text>
						</view>
						<view class="owner-info">
							<text class="label">房东信息：</text>
							<text class="value">{{detailInfo.homeowner_name}}；{{detailInfo.homeowner_phone}}</text>
						</view>
					</view>
				</view>
				<!-- <view class="switch-btn" @click="switchHouse">切换房源</view> -->
			</view>
		</view>

		<!-- 报修项目 -->
		<view class="repair-section">
			<view class="section-title">报修项目</view>
			<view class="repair-types">
				<view class="type-item" v-for="(item, index) in repairTypes" :key="index"
					:class="{ active: form.error_type === item.value }" @click="handleRadioClick(item.value)">
					<view class="radio-circle" :class="{ active: form.error_type === item.value }">
						<view class="inner-circle" v-if="form.error_type === item.value"></view>
					</view>
					<text class="label">{{ item.name }}</text>
				</view>
			</view>
		</view>

		<!-- 描述输入框 -->
		<view class="description-section">
			<view class="section-title">描述</view>
			<view class="textarea-box">
				<u--textarea height="70" v-model="form.error_title" placeholder="请具体描述报修情况..."></u--textarea>
			</view>
		</view>

		<!-- 图片上传 -->
		<view class="upload-section">
			<view class="section-title">上传图片 ({{imgList.length}}/3)</view>
			<view class="upload-box">
				<u-upload uploadIcon="photo" uploadIconColor="#0094f8" :fileList="imgList" @afterRead="afterRead"
					@delete="deletePic" :maxCount="3" :previewImage="true"></u-upload>
			</view>
		</view>

		<!-- 提交按钮 -->
		<view class="submit-btn" @click="submitForm">提交</view>
	</view>
</template>

<script>
	import {
		repairType,
		addRepair,
		LeaseDetail,
		LeaseDetailUser
	} from '@/api/lease.js'
	export default {
		data() {
			return {
				current: 0,
				description: "",
				repairTypes: [],
				imgList: [],
				room_id: '',
				detailInfo: {},
				form: {
					room_id: '',
					error_type: '',
					error_photo: '',
					error_title: '',
					error_photo_arr: [],
				}
			};
		},
		onLoad(e) {
			// 监听房源选择事件
			uni.$on('houseSelected', (house) => {
				// 更新当前房源信息
				this.currentHouse = house;
			});
			this.id = e.id
			this.room_id = e.room_id
			this.form.room_id = e.room_id
			this.getRepairType()
			this.getDetail()
		},
		methods: {
			getRepairType() {
				repairType().then((res) => {
					console.log(res)
					this.repairTypes = Object.entries(res.data.data).map(([key, value]) => ({
						name: value,
						value: key
					}));
				})
			},
			getDetail() {
				LeaseDetailUser({
					id: this.id
				}).then((res) => {
					this.detailInfo = res.data.data
				})
			},
			switchHouse() {
				uni.navigateTo({
					url: '/pages-lease/switch-house/switch-house'
				});
			},
			handleRadioClick(value) {
				this.form.error_type = value;
			},
			afterRead(e) {
				uni.showLoading({
					title: "上传中...",
				});
				this.imgList.push(e.file);
				this.uploadFile(e.file.url).then((res) => {
					uni.hideLoading();
					this.form.error_photo_arr.push(JSON.parse(res.data).code.fullurl);
				});

			},
			uploadFile(url) {
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: this.$Config.HTTP_REQUEST_URL + "/api/common/ossUpload", // 仅为示例，非真实的接口地址
						filePath: url,
						name: "file",
						formData: {
							file: url,
						},
						header: {
							token: uni.getStorageSync("userInfo").token,
						},
						success: (res) => {
							resolve(res);
						},
					});
				});
			},
			deletePic(e) {
				this.imgList.splice(e.index, 1);
				this.form.error_photo_arr.splice(e.index, 1);
			},
			submitForm() {
				if (this.form.error_type == '') {
					uni.showToast({
						title: "请选择报修类型",
						icon: "error", //如果要纯文本，不要icon，将值设为'none'
						duration: 2000, //持续时间为 2秒
					});
				} else if (this.form.error_title == '') {
					uni.showToast({
						title: "请描述损坏详情",
						icon: "error", //如果要纯文本，不要icon，将值设为'none'
						duration: 2000, //持续时间为 2秒
					});
				} else {
					if (this.form.error_photo_arr.length > 0) {
						this.form.error_photo = this.form.error_photo_arr.join(',')
					}
					delete this.form.error_photo_arr
					this.form.room_id = Number(this.form.room_id)
					addRepair(this.form).then((res) => {
						console.log(res.response.data.code)
						if (res.response.data.code == 1) {
							uni.showToast({
								title: res.response.data.msg,
								icon: "success", //如果要纯文本，不要icon，将值设为'none'
								duration: 2000, //持续时间为 2秒
							});
							setTimeout(() => {
								uni.navigateBack()
							}, 2500)
						}
					}).catch(err=>{
						console.log(err)
					})
				}
			}
		},
		onUnload() {
			// 移除事件监听
			uni.$off('houseSelected');
		}
	};
</script>

<style lang="less" scoped>
	.container {
		box-sizing: border-box;
		min-height: 100vh;
		background-color: #f5f6fa;
		padding-bottom: 40rpx;
	}

	.house-card {
		margin: 24rpx;
		background: #fff;
		border-radius: 12rpx;
		padding: 24rpx;

		.card-header {
			position: relative;

			.house-info {
				display: flex;
				// padding-right: 160rpx; // 为切换按钮留出空间

				.house-img {
					width: 160rpx;
					height: 160rpx;
					border-radius: 8rpx;
					margin-right: 24rpx;
				}

				.info-content {
					flex: 1;

					.title {
						font-size: 32rpx;
						font-weight: 500;
						color: #333;
						margin-bottom: 16rpx;
					}

					.room-info,
					.owner-info {
						font-size: 28rpx;
						margin-bottom: 12rpx;

						.label {
							color: #666;
						}

						.value {
							color: #333;
						}
					}
				}
			}

			.switch-btn {
				position: absolute;
				right: 0;
				top: 0;
				padding: 8rpx 24rpx;
				font-size: 28rpx;
				color: #2979ff;
				background: #fff;
				border: 2rpx solid #2979ff;
				border-radius: 8rpx;
			}
		}
	}

	.repair-section {
		margin: 24rpx;
		border-radius: 12rpx;

		.section-title {
			font-size: 28rpx;
			color: #333;
			margin-bottom: 24rpx;
		}

		.repair-types {
			gap: 24rpx;
			display: grid;
			padding: 30rpx 0;
			background: #fff;
			box-sizing: border-box;
			grid-template-columns: repeat(2, 1fr);

			.type-item {
				height: 40rpx;
				display: flex;
				align-items: center;
				padding: 0 32rpx;
				background: #fff;
				border-radius: 8rpx;

				&.active {
					border-color: #2979ff;
				}

				.radio-circle {
					width: 32rpx;
					height: 32rpx;
					border-radius: 50%;
					border: 2rpx solid #cccccc;
					margin-right: 16rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					box-sizing: border-box;
					transition: all 0.2s;

					&.active {
						border-color: #2979ff;
						border-width: 2rpx;

						.inner-circle {
							width: 16rpx;
							height: 16rpx;
							border-radius: 50%;
							background-color: #2979ff;
						}
					}
				}

				.label {
					font-size: 28rpx;
					color: #333;
				}
			}
		}
	}

	.description-section {
		margin: 24rpx;
		border-radius: 12rpx;

		.section-title {
			font-size: 28rpx;
			color: #333;
			margin-bottom: 24rpx;
		}

		.textarea-box {
			background: #ffffff;
			border-radius: 8rpx;
			padding: 24rpx;

			::v-deep .u-textarea {
				border: 0;
			}
		}
	}

	.upload-section {
		margin: 24rpx;
		background: #fff;
		border-radius: 12rpx;
		padding: 24rpx;

		.section-title {
			font-size: 28rpx;
			color: #333;
			margin-bottom: 24rpx;
		}

		.upload-box {
			.upload-btn {
				width: 160rpx;
				height: 160rpx;
				background: #f5f6fa;
				border-radius: 8rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}

	.submit-btn {
		margin: 48rpx 24rpx;
		height: 88rpx;
		line-height: 88rpx;
		text-align: center;
		background: #2979ff;
		border-radius: 44rpx;
		color: #fff;
		font-size: 32rpx;
	}
</style>